'use strict';

webappApp.controller('SearchCtrl', ['$scope', '$filter', 'geo', 'spotService', function ($scope, $filter, geo, spotService) {
  var toMarker = $filter('marker');
  $scope.results = spotService.results;
  $scope.$watch('results.spots', function (newSpots, oldSpots) {
    if (newSpots.length) {
      angular.forEach($scope.markers, function (marker) {
        marker.setMap(null);
      });
      $scope.markers = [];
      angular.forEach(newSpots, function (result) {
        $scope.markers.push(toMarker(result.location, $scope.map));
      });
    }
  });
  $scope.initMap = function (map) {
    geo.location().then(function (loc) {
      map.setZoom(19);
      map.setCenter(new google.maps.LatLng(loc.latitude, loc.longitude));
    });
    map.unbind('idle');
  };
}]);